define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'trades/order/order/index' + location.search,
                   // add_url: 'trades/order/order/add',
                  //  edit_url: 'trades/order/order/edit',
                 //   del_url: 'trades/order/order/del',
                    multi_url: 'trades/order/order/multi',
                    import_url: 'trades/order/order/import',
                    table: 'activity_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                       // {checkbox: true},
                       // {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left', operate: 'LIKE %...%', placeholder: '活动名称搜索'},
                        {field: 'orderid', title: __('Orderid'), operate: 'LIKE'},
                        //{field: 'account_id', title: __('Account_id')},

                        //{field: 'activity_id', title: __('Activity_id')},

                        // {field: 'paytype_text', title: __('Paytype'), operate: false,formatter:function(value,row){
                        //         if (row['paytype'] == 1)
                        //         {
                        //             return '<span style="color:yellowgreen">'+value+'</span>';
                        //         }else {
                        //             return '<span style="color:black">'+value+'</span>';
                        //         }
                        //     }
                        // },
                        {field: 'paytime', title: __('Paytime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'state',
                            title: __('State') ,
                            visible:false,
                            formatter: Table.api.formatter.normal,
                            searchList: { "1":'待支付',"2":'已支付',"4":'已退款'},
                        },
                        {
                            field: 'state_text',
                            title: __('State'),
                            operate:false,
                            formatter:function(value,row) {
                                if (row['state'] == 1) {
                                    return '<span style="color:black">' + value + '</span>';
                                } else if (row['state'] == 2) {
                                    return '<span style="color:yellowgreen">' + value + '</span>';
                                } else if (row['state'] == 3) {
                                    return '<span style="color:red">' + value + '</span>';
                                } else if (row['state'] == 4) {
                                    return '<span style="color:red">' + value + '</span>';
                                }
                            }
                        },
                        {field: 'num', title: __('入场劵数量'),operate:false},//,formatter:Controller.api.formatter._Ticket

                        {field: 'amount', title: __('Amount'),operate:false},
                        {field: 'createTime', title: __('Createtime'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'ordertype_text', title: __('Ordertype'), operate: false,formatter:function(value,row){
                                if (row['ordertype'] == 1)
                                {
                                    return '<span style="color:yellowgreen">'+value+'</span>';
                                }else {
                                    return '<span style="color:black">'+value+'</span>';
                                }
                            }
                        },
                        {field: 'buyer_id', title: __('Buyer_id'),operate:false},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            buttons: [
                                {
                                    name: 'detail',
                                    text: '详情',
                                    icon: 'fa fa-folder-o',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'trades/order/order/detail?ids={ids}',
                                },
                                {
                                    name: 'ajax',
                                    text: '退款',
                                    title: __('发送请求'),
                                    classname: 'btn btn-xs btn-primary btn-dialog btn-check',
                                    icon: 'fa fa fa-send',
                                    url: 'trades/order/order/operate?ids={ids}',
                                    hidden:function(row){
                                        if(row['state'] != 2){
                                            return true;
                                        }
                                    }
                                }
                            ],
                            table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter:{
                _Ticket:function (value,row,index){
                    var titles = '优惠劵';
                    $.ajax({ url:"trades/order/order/getTicketInfo",
                        data:{oid:row['id']},
                        success:function(data){
                           console.log(data);
                        }

                    });
                    return '<a href="javascript:;" data-toggle="tooltip" data-title="'+titles+'">'+ value+'</a>';
                }
            }
        }
    };
    return Controller;
});